<?php include "../public/header.php"?>
<?php include "../public/header_public.php"?>
<style>
    div[lay-id="datalist"] .layui-table-cell{
        height:auto !important;
    };
</style>
<body class="laytp-container">
    <div style="height: 45px"></div>
    <div id="opt_nav">
    </div>
    <div id="dialog">
        <div class="page-title" id="page-title">
            <div class="page-title-con">
                <span class="title">选择证书信息</span>
                <div class="move_title"></div>
                <div class="page-title-opt-list">
                    <button class="layui-btn layui-btn-sm btn-dark marginr5" type="button" id="submit">确定</button>
                    <span id="page-close" class="page-close" onclick="layer_close()">×</span>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-card" id="search-form">
        <div class="layui-card-body">
            <form class="layui-form" id="TableSearchFormId" lay-filter="TableSearchForm" onkeydown="keyTableSearch('TableSearchForm', 'datalist')">
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-md3">
                            <label class="layui-form-label">所属单位</label>
                            <div class="layui-input-block">
                                <div id="chanid" class="xmselect_div">
                                    <div class="xmselect_title">请选择</div>
                                </div>
                            </div>
                        </div>

                        <div class="layui-col-md3">
                            <label class="layui-form-label">人员姓名</label>
                            <div class="layui-input-block">
                                <input type="text" name="person_name" placeholder="请输入" class="layui-input" autocomplete="off">
                            </div>
                        </div>
                        <div class="layui-col-md3">
                            <label class="layui-form-label">证书名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" id="name" placeholder="请输入" class="layui-input" autocomplete="off">
                            </div>
                        </div>
                        <div class="layui-col-md3 search_hide none">
                            <label class="layui-form-label">证书编号</label>
                            <div class="layui-input-block">
                                <input type="text" name="number" placeholder="请输入" class="layui-input" autocomplete="off">
                            </div>
                        </div>
                        <div class="layui-col-md3 search_hide none">
                            <label class="layui-form-label">证书专业</label>
                            <div class="layui-input-block">
                                <input type="text" name="major" id="major" placeholder="请输入" class="layui-input" autocomplete="off">
                            </div>
                        </div>


                        <div class="layui-col-md3 search_hide none">
                            <label class="layui-form-label">人员性质</label>
                            <div class="layui-input-block">
                                <div id="person_nature" class="xmselect_div">
                                    <div class="xmselect_title">请选择</div>
                                </div>
                            </div>
                        </div>


                        <div class="layui-col-md3 search_hide none">
                            <label class="layui-form-label">证书去向</label>
                            <div class="layui-input-block">
                                <select name="goes" id="goes"></select>
                            </div>
                        </div>
                        <div class="layui-col-md3 search_hide none">
                            <label class="layui-form-label">使用情况</label>
                            <div class="layui-input-block">
                                <select name="use" id="use"></select>
                            </div>
                        </div>
                        <div class="layui-col-md3 search_hide none">
                            <label class="layui-form-label">人员状态</label>
                            <div class="layui-input-block">
                                <select name="person_status" id="person_status"></select>
                            </div>
                        </div>
                        <div class="layui-col-md3 search_hide none">
                            <label class="layui-form-label">社保状态</label>
                            <div class="layui-input-block">
                                <div id="social" class="xmselect_div">
                                    <div class="xmselect_title">请选择</div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md3">
                            <div class="layui-form-item search-div">
                                <div class="layui-form-item layui-inline">
                                    <input type="hidden" id="type" name="type" value="">
                                    <button class="laytp-btn laytp-btn-md laytp-btn-primary" id="TableSearchButton" type="button"><i class="layui-icon layui-icon-search"></i>查询</button>
                                    <button class="layui-btn layui-btn-primary" onclick="listTableReset()" id="TableSearchReset" type="reset" ><i class="layui-icon layui-icon-refresh"></i>重置</button>
                                    <div id="search_open" onclick="toggle_search()">
                                        <i class="layui-icon layui-icon-down"></i>展开
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>


    <div class="layui-card" id="table-card">
        <div class="layui-card-body">
            <div class="layui-row">
                <?php include '../cert/dao.php' ?>

                <div class="layui-col-md10" id="table_list">
                    <div class="layui-card-body">
                        <table id="datalist" lay-filter="datalist"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript">
        // 重选设置默认选中
        var oldData = getQueryVariable('old_data');
        var oldDataIdArr = [];
        if(oldData){
            oldDataIdArr = oldData.split(',');
        }
        // 单选多选分类 默认单选 choose_type传值为多选
        var choose_type = getQueryVariable('choose_type');
        if(choose_type === undefined || choose_type === false){
            choose_type = 'radio';
        }else{
            choose_type = 'checkbox';
        }
        // 是否赋值在同一个节点上 默认赋值两个节点
        var input = getQueryVariable('input');
        if(input === undefined || input === false){
            input = 'multi';
        }else{
            input = 'single';
        }

        // 页面绑定节点元素
        var parent_dom = getQueryVariable('dom');
        // 页面分类
        var parent_page = getQueryVariable('page');
        // 明细
        var parent_index = getQueryVariable('index');

        // 传递参数
        var parent_status = getQueryVariable('status');
        if(parent_status === undefined || parent_status === false){
            parent_status = '';
        }
        var parent_goes = getQueryVariable('goes');
        if(parent_goes === undefined || parent_goes === false){
            parent_goes = '';
        }

        var issuoArr = []; var placeArr = [];
        var currButAuth, currTabData, treeObj, treeData = [], treeStatus = 2, sea_chanid=''
            , queryStatus = 0, sea_person_nature='';
        layui.use(['table','form','element','facade'],function() {
            var table = layui.table;
            var facade = layui.facade;
            var form = layui.form;


            $('#TableSearchReset').click(function () {
                sea_chanid='';sea_person_nature='';
                $('.xmselect_div>xm-select').find('.xm-label-block').css('display','none');
                $('.xmselect_div').append('<div class="xmselect_title">请选择</div>');
                searchTableFunc(form, table)
            });


            $("#cols_table").hide();
            $("#export_table").hide();
            table.render({
                elem: '#datalist'
                ,toolbar: false
                ,defaultToolbar: ['', '', '']
                ,method:'post'
                ,url: facade.url(ask_opt_get_cert)
                ,page: true
                ,where: {status: parent_status, goes: parent_goes}
                ,request: {pageName: 'page',limitName: 'pagesize'}
                ,parseData: function(res){
                    issuoArr = res.data.issuo;
                    placeArr = res.data.place;

                    if(queryStatus === 0){




                        var goesArr = res.data.goes;
                        var goes_str = '<option value="">请选择</option>';
                        for(var i in goesArr){
                            goes_str += '<option value="'+i+'">'+goesArr[i]+'</option>';
                        }
                        $('#goes').html(goes_str);

                        var useArr = res.data.use;
                        var use_str = '<option value="">请选择</option>';
                        for(var i in useArr){
                            use_str += '<option value="'+i+'">'+useArr[i]+'</option>';
                        }
                        $('#use').html(use_str);

                        var person_statusArr = res.data.person_status;
                        var person_status_str = '<option value="">请选择</option>';
                        for(var i in person_statusArr){
                            person_status_str += '<option value="'+i+'">'+person_statusArr[i]+'</option>';
                        }
                        $('#person_status').html(person_status_str);

                        queryStatus = 1;
                    }

                    xmselect_dan('chanid', res.data.chanid, sea_chanid, function (res) {
                        sea_chanid = res.arr && res.arr.length > 0 ?  res.arr[0].value : '';
                        searchTableFunc(form, table);
                    });
                    xmselect_dan('person_nature', res.data.person_nature, sea_person_nature, function (res) {
                        sea_person_nature = res.arr && res.arr.length > 0 ?  res.arr[0].value : '';
                        searchTableFunc(form, table);
                    });

                    xmselect_dan('social', res.data.social, res.data.is_social, function (res) {
                        setTimeout(function () {
                            searchTableFunc(form, table);
                        },100)
                    });

                    render_from();
                    return {
                        "code": res.code,
                        "msg": res.message,
                        "count": res.data.data.total,
                        "data": res.data.data.data
                    };
                }
                ,response: {statusCode: 2000}
                ,cols: [[
                    {type:choose_type,fixed:'left'}
                    ,{type:'numbers',title:'序号',fixed:'left'}
                    ,{field:'person_name',title:'证书持证人'}
                    ,{field:'chanid_name',title:'所属单位'}
                    ,{field:'person_nature',title:'人员性质'}
                    ,{field:'name',title:'证书名称'}
                    ,{field:'major',title:'证书专业'}
                    ,{field:'number',title:'证书编号'}
                    ,{field:'expiry_dt',title:'有效期止'}
                    ,{field: 'social', title: '社保状态'}
                    ,{field: 'socialmonth', title: '参保月份数'}
                    ,{field:'status_name',title:'证书状态'}
                    ,{field:'goes_name',title:'证书去向'}
                    ,{field:'use_name',title:'使用情况'}
                ]]
                // 重选设置默认选中
                ,done : function(res, curr, count){
                    if( oldDataIdArr ){
                        layui.each(res.data, function(index,item){
                            if( oldDataIdArr.indexOf(''+item.id+'') > -1 ){
                                if(choose_type === 'radio'){
                                    res.data[index]["LAY_CHECKED"] = 'true';
                                    $("div[lay-id='datalist'] tr[data-index=" + index + "]").attr("class", "layui-table-click");
                                    $("div[lay-id='datalist'] tr[data-index=" + index + "]").find(".layui-form-radio").attr("class", "layui-unselect layui-form-radio layui-form-radioed");
                                    $("div[lay-id='datalist'] tr[data-index=" + index + "]").find(".layui-icon").attr("class", "layui-anim layui-icon layui-anim-scaleSpring");
                                    $("div[lay-id='datalist'] tr[data-index=" + index + "] input[type='radio']").next().click();
                                }else if(choose_type === 'checkbox'){
                                    res.data[index]["LAY_CHECKED"] = 'true';
                                    $("div[lay-id='datalist'] tr[data-index=" + index + "] input[type='checkbox']").prop('checked', true);
                                    $("div[lay-id='datalist'] tr[data-index=" + index + "] input[type='checkbox']").next().addClass('layui-form-checked');
                                }
                            }
                        });
                    }

                    // 状态不是空闲的禁止选中
                    var check = 0;
                    res.data.forEach(function (item, index) {
                        if(item.name.indexOf('建造师') != -1){
                            if(item.use != 1){
                                $('div[lay-id="datalist"] .layui-table-fixed-l .layui-table-body div[class="layui-unselect layui-form-checkbox"]')
                                    .eq(index).css({'pointer-events':'none'});
                                $('div[lay-id="datalist"] .layui-table-fixed-l .layui-table-body div[class="layui-unselect layui-form-checkbox"]')
                                    .eq(index).parent().css({'cursor':'not-allowed'});
                                check = 1;
                            }
                        }
                    });
                    if(check == 1){
                        $('div[lay-id="datalist"] .layui-table-fixed-l .layui-table-header div[class="layui-unselect layui-form-checkbox"]')
                            .css({'pointer-events':'none'});
                        $('div[lay-id="datalist"] .layui-table-fixed-l .layui-table-header div[class="layui-unselect layui-form-checkbox"]')
                            .parent().css({'cursor':'not-allowed'});
                    }else{
                        $('div[lay-id="datalist"] .layui-table-fixed-l .layui-table-header div[class="layui-unselect layui-form-checkbox"]')
                            .css({'pointer-events':'auto'});
                        $('div[lay-id="datalist"] .layui-table-fixed-l .layui-table-header div[class="layui-unselect layui-form-checkbox"]')
                            .parent().css({'cursor':'auto'});
                    }

                    // 处理列表fixed的高度问题
                    $('div[lay-id="datalist"] .layui-table-main>table>tbody>tr').each(function (i) {
                        var height = $(this).height();
                        $('div[lay-id="datalist"] .layui-table-fixed-l>.layui-table-body>table>tbody>tr')
                            .eq(i).css('cssText','height:'+height+'px !important');
                        $('div[lay-id="datalist"] .layui-table-fixed-r>.layui-table-body>table>tbody>tr')
                            .eq(i).css('cssText','height:'+height+'px !important');
                    });
                }
            });

            listTableSearch(form, table);

            // 查询数据-以前的查询条件都会保留
            $('#search').on('click', function(){
                var keyword = $('#keyword').val();
                table.reload('datalist', {
                    page:{curr:1},
                    where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                        keyword:keyword
                    }
                });
            });

            $('#submit').on('click', function () {
                var checkStatus = layui.table.checkStatus('datalist');
                var data = checkStatus.data;
                if(data.length === 0){
                    facade.error('请选择数据'); return false;
                }


                if(parent_page === 'cert_use'){
                    var type = getQueryVariable('type');
                    var str = '';
                    var length = $("#addDiv .short_table_tbody tr" , parent.document).length;
                    for(var i=0;i<data.length;i++){
                        str += '<tr>';
                        str += '<td style="width: 45px">'+(length+i+1)+'</td>';
                        str += '<td>';
                        str += '<select class="layui-select place">';
                        str += '<option value="">请选择</option>';
                        placeArr.forEach(function (v) {
                            str += '<option value="'+v+'">'+v+'</option>';
                        });
                        str += '</select>';
                        str += '<input type="hidden" class="layui-input cert_id" value="'+data[i].id+'">';
                        str += '</td>';
                        // str += '<td>' +
                        //     '<input type="text" class="layui-input place">' +
                        //     '<input type="hidden" class="layui-input cert_id" value="'+data[i].id+'">' +
                        //     '</td>';
                        str += '<td>'+data[i].name+'</td>';
                        str += '<td>'+data[i].number+'</td>';
                        str += '<td>'+data[i].major_name+'</td>';
                        str += '<td>'+data[i].person_name+'</td>';
                        str += '<td>'+data[i].use_name+'</td>';
                        str += '<td>'+data[i].com_name+'</td>';
                        if(type == 1){ // 投标用证
                            str += '<td class="issuo_status none">';
                        }else{
                            str += '<td class="issuo_status">';
                        }
                        str += '<select class="layui-select issuo">';
                        str += '<option value=""></option>';
                        for (var j in issuoArr){
                            str += '<option value="'+j+'">'+issuoArr[j]+'</option>';
                        }
                        str += '</select>';
                        str += '</td>';

                        if(type == 1){ // 投标用证
                            str += '<td class="usefee_status none"></td>';
                            str += '<td class="startdt_status none"></td>';
                            str += '<td class="yudt_status none"></td>';
                        }else{
                            str += '<td class="usefee_status"><input type="number" class="layui-input usefee"></td>';
                            str += '<td class="startdt_status"><input type="text" class="layui-input startdt bdate_ymd" readonly></td>';
                            str += '<td class="yudt_status"><input type="text" class="layui-input yudt bdate_ymd" readonly></td>';
                        }

                        str += '</tr>';
                    }
                    $("#addDiv .short_table_tbody" , parent.document).append(str);
                    parent.render_from();
                    parent.update_refresh_row_func();
                }

                if(parent_page === 'cert_use_change'){
                    var index = getQueryVariable('index'); // 判断位置，存在则是新增，不存在则是替换
                    if(index === undefined || index === false){
                        // 变更选择处理
                        var cert_name = decodeURI(getQueryVariable('cert_name'));
                        var person_name = decodeURI(getQueryVariable('person_name'));
                        var place = decodeURI(getQueryVariable('place'));
                        var cert_id = getQueryVariable('cert_id');
                        var id = getQueryVariable('id');
                        var issuo = decodeURI(getQueryVariable('issuo'));
                        var type = getQueryVariable('type');
                        var length = $("#optDiv #show_tbody tr" , parent.document).length;
                        var str = '';
                        for(var i=0;i<data.length;i++){
                            str += '<tr>';
                            str += '<td style="width: 45px">'+(length+i+1)+'</td>';
                            str += '<td>';
                            str += '<select class="layui-select place">';
                            str += '<option value="">请选择</option>';
                            placeArr.forEach(function (v) {
                                if(place == v){
                                    str += '<option value="'+v+'" selected>'+v+'</option>';
                                }else{
                                    str += '<option value="'+v+'">'+v+'</option>';
                                }
                            });
                            str += '</select>';
                            str += '<input type="hidden" class="cert_id" value="'+data[i].id+'">';
                            str += '<input type="hidden" class="pid" value="'+id+'">';
                            str += '</td>';
                            // str += '<td><input type="text" class="layui-input place" value="'+place+'">' +
                            //     '<input type="hidden" class="cert_id" value="'+data[i].id+'">' +
                            //     '<input type="hidden" class="pid" value="'+id+'">' +
                            //     '</td>';
                            str += '<td>'+data[i].name+'</td>';
                            str += '<td>'+data[i].number+'</td>';
                            str += '<td>'+data[i].major_name+'</td>';
                            str += '<td>'+data[i].person_name+'</td>';
                            str += '<td>'+data[i].use_name+'</td>';
                            str += '<td>'+data[i].com_name+'</td>';
                            if(type == 2){
                                str += '<td class="issuo_status"><select class="layui-select issuo">';
                                if(issuo === '是'){
                                    str += '<option value="">请选择</option><option value="1" selected>是</option><option value="2">否</option>';
                                }else if(issuo === '否'){
                                    str += '<option value="">请选择</option><option value="1">是</option><option value="2" selected>否</option>';
                                }else{
                                    str += '<option value="">请选择</option><option value="1">是</option><option value="2">否</option>';
                                }
                                str += '</select></td>';
                            }
                            if(type == 2){
                                str += '<td class="usefee_status"><input type="number" class="layui-input usefee"></td>';
                                str += '<td class="startdt_status"><input type="text" class="layui-input startdt bdate_ymd" readonly></td>';
                                str += '<td class="yudt_status"><input type="text" class="layui-input yudt bdate_ymd" readonly></td>';
                            }
                            str += '<td>替换:'+person_name+'('+cert_name+')</td>';
                            str += '</tr>';
                        }
                        $("#optDiv #show_tbody" , parent.document).append(str);
                        parent.render_from();
                        parent.update_refresh_row_func();
                    }else{
                        // 新增选择处理
                        $("#optDiv #show_tbody tr", parent.document).eq(index - 1).find('.cert_id').val(data[0].id);
                        $("#optDiv #show_tbody tr", parent.document).eq(index - 1).find('#cert_name').text(data[0].name);
                        $("#optDiv #show_tbody tr", parent.document).eq(index - 1).find('#cert_number').text(data[0].number);
                        $("#optDiv #show_tbody tr", parent.document).eq(index - 1).find('#cert_major').text(data[0].major_name);
                        $("#optDiv #show_tbody tr", parent.document).eq(index - 1).find('#person_name').text(data[0].person_name);
                        $("#optDiv #show_tbody tr", parent.document).eq(index - 1).find('#use').text(data[0].use_name);
                        $("#optDiv #show_tbody tr", parent.document).eq(index - 1).find('#com_name').text(data[0].com_name);

                        var str = '';
                        str += '<select class="layui-select issuo">';
                        str += '<option value=""></option>';
                        for (var j in issuoArr){
                            str += '<option value="'+j+'">'+issuoArr[j]+'</option>';
                        }
                        str += '</select>';
                        $("#optDiv #show_tbody tr" , parent.document).eq(index - 1).find('#issuo').html(str);
                        parent.render_from();
                        parent.update_refresh_row_func();
                    }
                }

                if(!parent_page){
                    $("#"+parent_dom+'_name' , parent.document).val(data[0].person_name+'('+data[0].name+')');
                    $("#"+parent_dom , parent.document).val(data[0].id);
                }

                layer_close();
            });

            // 清空
            $('#empty').on('click', function () {
                $("#"+parent_dom+'_name' , parent.document).val('');
                $("#"+parent_dom , parent.document).val('');
                layer_close();
            });
        });
    </script>
</body>
</html>
